import {
	baseURL,
	platform,
    uniacid
} from "@/common/config.js"

const install = (Vue, vm) => {



    Vue.prototype.$u.http.setConfig({
        baseUrl: baseURL,
        loadingText: '努力加载中~',
        loadingTime: 5000,
        showLoading: true,
        loadingMask: true,
    });

    // 请求拦截
    Vue.prototype.$u.http.interceptor.request = (config) => {
        config.header.Token = vm.vuex_token;
        config.header.platform = platform;
        config.header.uniacid = uniacid;

        if (config.url.indexOf("?") != -1) {
            config.url += '&uniacid=' + uniacid;
        } else {
            config.url += '?uniacid=' + uniacid;
        }

        return config;
    }

    // 响应拦截
    Vue.prototype.$u.http.interceptor.response = (res) => {
        if (res.code == 200) {
            return res.data;
        } else if (res.code == 401) {
            vm.$u.toast('验证失败，请重新登录');
            vm.$u.vuex('vuex_token', null)
            vm.$u.vuex('vuex_user', null)

            setTimeout(() => {
                uni.reLaunch({
                    url: '/pages/index/index'
                })
            }, 800)

            return false;
        } else {
            setTimeout(() => {
                vm.$u.toast(res.msg)
            }, 300)
            return false;
        }
    }
}

export default {
    install
}


